﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Jzy.Workflow.Line;
using Frame.Lib.Core.Base;
using Frame.Lib.Core;

namespace Jzy.Workflow.Node
{
    public class FlowNodeDAL : BaseDAL
    {
        public List<FlowNodeInfo> GetList(int flowID)
        {
            string sql = "select * from " + WFTableName.FlowNode + " where FlowID = @p0";
            List<FlowNodeInfo> list = dbOper.Query<FlowNodeInfo>(sql, flowID);
            return list;
        }

        public DataTable GetDataTable(int flowID)
        {
            string sql = "select * from " + WFTableName.FlowNode + " where FlowID = @p0";
            DataTable list = dbOper.Query(sql, flowID);
            return list;
        }

        public void SavePosition(int id, double left, double top)
        {
            string sql = "update " + WFTableName.FlowNode + " set [Left] = @p1, [Top] = @p2 where ID = @p0";
            dbOper.RunSql(sql, id, left, top);
        }

        internal List<FlowNodeInfo> GetNextNodes(object id)
        {
            string sql = "select * from " + WFTableName.FlowNode + " where ID in (Select ToNodeID from " + WFTableName.FlowLine + " where FromNodeID = @p0)";

            List<FlowNodeInfo> nodes = dbOper.Query<FlowNodeInfo>(sql, id);
            return nodes;
        }

        internal List<int> GetUserIDs(object id)
        {
            string sql = "select UserID From " +WFTableName.FlowNodeUser + " where NodeID = @p0";
            List<int> userIDs = dbOper.Query<int>(sql, id);
            return userIDs;
        }

        /// <summary>
        /// 获取一个用户在一个流程当中可能审批的节点
        /// </summary>
        /// <param name="flowID"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<FlowNodeInfo> GetList(int flowID, int userID)
        {
            string sql = "select * from " + WFTableName.FlowNode + " where FlowID=@p0 and " +
                         "ID in ( select NodeID from " + WFTableName.FlowNodeUser + " where UserID=@p1 union  select NodeID from " + WFTableName.FlowNodeRole + " where RoleID in" +
                         "(select RoleID from " + STableName.RoleUser + " where UserID= @p1))";

            List<FlowNodeInfo> list = dbOper.Query<FlowNodeInfo>(sql, flowID, userID);
            return list;
        }

        internal List<int> GetRoleIDs(object id)
        {
            string sql = "select RoleID from " + WFTableName.FlowNodeUser + " where NodeID = @p0";
            List<int> roleIDs = dbOper.Query<int>(sql, id);
            return roleIDs;
        }

        internal void SaveUserIDs(List<int> userIDs,object id)
        {
            if (userIDs == null)
            {
                return;
            }
            string sql = "delete from " + WFTableName.FlowNodeUser + " where NodeID = @p0";
            dbOper.RunSql(sql, id);
            foreach (int userID in userIDs)
            {
                sql = "insert into " + WFTableName.FlowNodeUser + "(NodeID, UserID) values (@p0, @p1)";
                dbOper.RunSql(sql, id, userID);
            }
            
        }

        internal void SaveRoleIDs(List<int> roleIDs,object id)
        {
            if (roleIDs == null)
            {
                return;
            }
            string sql = "delete from " + WFTableName.FlowNodeUser + " where NodeID = @p0";
            dbOper.RunSql(sql, id);
            foreach (int roleID in roleIDs)
            {
                sql = "insert into " + WFTableName.FlowNodeUser + "(NodeID, RoleID) values(@p0, @p1)";
                dbOper.RunSql(sql, id, roleID);
            }
        }



        public List<FlowLineInfo> GetToLines(int id)
        {
            string sql = "select * from " + WFTableName.FlowLine + " where ToNodeID =@p0";
            List<FlowLineInfo> lines = dbOper.Query<FlowLineInfo>(sql, id);
            return lines;
        }

        public List<FlowLineInfo> GetFromLines(int id)
        {
            string sql = "select * from " + WFTableName.FlowLine + " where FromNodeID=@p0";
            List<FlowLineInfo> lines = dbOper.Query<FlowLineInfo>(sql, id);
            return lines;
        }
    }

}
